package test.redis;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Properties;

/**
 * @Author: sunshuo
 * @Date: 2019/8/8/008 18:46
 * @Version: 1.0
 */
public class RedisProperties {

    private static Properties properties;

    private static Properties getProperties() {
        if (properties == null) {
            synchronized (RedisProperties.class) {
                try {
                    properties = PropertiesLoaderUtils.
                            loadProperties(new ClassPathResource("redis.properties"));
                } catch (IOException e) {
                    throw new RuntimeException("加载redis.properties文件失败", e);
                }
            }
        }
        return properties;
    }

    public static String getString(String key, String defaultValue) {
        return getProperties().getOrDefault(key, defaultValue).toString().trim();
    }

    public static Integer getInteger(String key, Integer defaultValue) {
        return Integer.valueOf(getProperties().getOrDefault(key, defaultValue).toString().trim());
    }

    public static Boolean getBoolean(String key, Boolean defaultValue) {
        return Boolean.valueOf(getProperties().getOrDefault(key, defaultValue).toString().trim());
    }

    public static Double getDouble(String key, Double defaultValue) {
        return Double.valueOf(getProperties().getOrDefault(key, defaultValue).toString().trim());
    }
}
